﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ISolutions.SharePoint.Mui.Impl.Resources
{
    /// <summary>
    /// Mui Ressource Service
    /// </summary>
    class MuiResourceService
    {
        public string URL_PARAM_RESKEY = "res";

        /// <summary>
        /// Gets the MUI resource.
        /// </summary>
        /// <param name="spResource">The sp resource.</param>
        /// <returns></returns>
        public static MuiResource GetMuiResource(SPUserResource spResource)
        {
            MuiResource newResource = new MuiResource(spResource.Name);
            newResource.InitializeResource(spResource);
            return newResource;
        }

        /// <summary>
        /// Gets the MUI resource.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static MuiResource GetMuiResource(string key)
        {
            return GetMuiResource(SPContext.Current.Web.UserResources[key]);
        }

        /// <summary>
        /// Returns true if specified name exists in UserResources
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool Exists(SPWeb web, string reference)
        {
            return web.UserResources[reference] != null;
        }

        /// <summary>
        /// Returns the SPUserResource by the specified name. 
        /// If no SPUserResource with the specified name exists, null will be returned.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static SPUserResource Find(SPWeb web, string name)
        {
            return web.UserResources[name];
        }

        /// <summary>
        /// Gets the SPUserResource from specified web. If resource does not exists it will be created.
        /// </summary>
        /// <param name="web"></param>
        //public void GetResource(SPWeb web)
        //{
        //    SPUserResource uRes = MuiResource.Find(web, this.Key);

        //    if (uRes != null)
        //    {
        //        InitializeResource(uRes);
        //    }
        //    else
        //    {
        //        this.Save(web);
        //    }
        //}

        //    private string _exportPath = HttpContext.Current.Server.MapPath(string.Format("~/App_GlobalResources/mui_{0}_{1}_export.resx",
        //SPContext.Current.Web.Title, System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName));
        //    private string _importPath = HttpContext.Current.Server.MapPath(string.Format("~/App_GlobalResources/mui_{0}_import.resx",
        //        SPContext.Current.Web.Title, System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName));

        //    protected List<MuiResource> ExportMuiResources()
        //    {
        //        List<MuiResource> muiResources = new List<MuiResource>();
        //MuiResource res = new MuiResource("Test111", "Blablabla");
        //res.Comment = "Single line of text";
        //muiResources.Add(res);

        //// export resx 
        //using (FileStream file = File.Create(_exportPath))
        //{
        //    SPContext.Current.Web.ExportUserResources(new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID), true, file);
        //}
        //// read exported file
        //using (FileStream file = File.OpenRead(_exportPath))
        //{
        //    using (ResXResourceReader reader = new ResXResourceReader(file))
        //    {
        //        reader.UseResXDataNodes = true;
        //        foreach (DictionaryEntry entry in reader)
        //        {
        //            if (entry.Key.ToString() != _langNode)
        //            {
        //                ResXDataNode dataNode = (ResXDataNode)entry.Value;
        //                MuiResource res = new MuiResource(dataNode.Name, dataNode.GetValue((ITypeResolutionService)null).ToString());
        //                res.Comment = dataNode.Comment;
        //                muiResources.Add(res);
        //            }
        //        }
        //    }
        //}
        //File.Delete(_exportPath);

        //    return muiResources;
        //}

        //protected void ImportMuiResources()
        //{
        //ResXResourceWriter writer = new ResXResourceWriter(_importPath);
        //ResXDataNode langNode = new ResXDataNode(_langNode, System.Threading.Thread.CurrentThread.CurrentUICulture.LCID.ToString());
        //langNode.Comment = string.Empty;
        //writer.AddResource(langNode);
        //foreach (GridViewRow row in this.muiContent.Rows)
        //{
        //    ResXDataNode node = new ResXDataNode((row.FindControl("lblName") as Label).Text, (row.FindControl("txtValue") as TextBox).Text);
        //    node.Comment = (row.FindControl("ddlComment") as DropDownList).SelectedValue;
        //    if (node.Name != "ExportedLanguage")
        //    {
        //        writer.AddResource(node);
        //    }
        //}
        //writer.Generate();
        //writer.Close();

        //using (FileStream file = new FileStream(_importPath, FileMode.Open))
        //{
        //    try
        //    {
        //        SPContext.Current.Web.ImportUserResources(new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID), file);
        //    }
        //    catch (Exception ex)
        //    {
        //        this.lblErrorMsg.Text = ex.Message;
        //    }
        //}

        //File.Delete(_importPath);
        //}
    }
}
